package com.airnews.ticket.util;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Environment;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.airnews.ticket.R;

public class Utils {
	
	public static DecimalFormat df2 = new DecimalFormat("####0.00"); 

	/**
	 * 退出应用
	 * 
	 * @param activity
	 */
	public static void exitApplication(final Activity activity) {
		final ProgressDialog progressDialog = ProgressDialog.show(activity, null, 
							activity.getString(R.string.exit_app), true);
		new Thread(){
			@Override
			public void run() {
				//BaseResult baseResult = NetAccessor.logout(activity);
				final ActivityManager am = (ActivityManager) activity.getSystemService(Context.ACTIVITY_SERVICE);
				am.restartPackage(activity.getPackageName());
				progressDialog.dismiss();
				activity.finish();
			}
		}.start();
	}

	/**
	 * 退出系统确认对话框
	 * 
	 * @param context
	 * @param msg
	 */
	public static void exitAppDialog(final Activity activity) {
		new AlertDialog.Builder(activity)
				//.setIcon(R.drawable.alert_dialog_icon)
				.setMessage(activity.getString(R.string.confirm_exist))
				.setTitle(R.string.alert_dialog_title)
				.setPositiveButton(R.string.alert_dialog_yes,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
								Utils.exitApplication(activity);
							}
						})
				.setNegativeButton(R.string.alert_dialog_cancel,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int whichButton) {
							}
						}).show();
	}

	
	
	
	
	/**
	 * 取最近n个月的列表
	 * @return
	 */
	public static ArrayList<String> getLatestMonth(int n){
		SimpleDateFormat mDateFormat = new SimpleDateFormat("yyyy-MM");
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(System.currentTimeMillis());
		ArrayList<String> months = new ArrayList<String>(n);
		for(int i=0; i < n; i++){
			if(i > 0 ){
				cal.add(Calendar.MONTH, -1);
				months.add(mDateFormat.format(cal.getTime()));
			}else{
				months.add(mDateFormat.format(cal.getTime()));
			}
		}
		return months;
	}
	
	
	/**
	 * 日期按月推移
	 * @param addMonth
	 * @param format
	 * @return
	 */
	public static String getAddMonthDate(int addMonth, String format){
		SimpleDateFormat mDateFormat = new SimpleDateFormat(format);
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(System.currentTimeMillis());
		cal.add(Calendar.MONTH, addMonth);
		return mDateFormat.format(cal.getTime());
	}
	
	
	/**
	 * 隐藏键盘
	 * @param ctx
	 * @param v
	 */
	public static void hiddenKeyboard(Context ctx, View v){
		InputMethodManager imm = (InputMethodManager)ctx.getSystemService(Context.INPUT_METHOD_SERVICE);  
 	   imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
	}
	
	
	/**
	 * 公共业务办理对话框
	 * @param activity
	 * @param title
	 * @param msg
	 * @param listener
	 * @param showCancel
	 */
	public static void showDialog(final Activity activity, String title, 
			String msg, DialogInterface.OnClickListener listener, boolean showCancel) {
		Builder builder = new AlertDialog.Builder(activity)
		.setTitle(title)
		.setMessage(msg)
		.setPositiveButton(R.string.alert_dialog_yes,listener);
		if(showCancel){
			builder.setNegativeButton(R.string.alert_dialog_cancel,null);
		}
		builder.show();
	}
	
	
	/**
	 * 显示Toast
	 * @param ctx
	 * @param msg
	 */
	public static void showToast(Context ctx, String msg){
		Toast.makeText(ctx, msg, Toast.LENGTH_LONG).show();
	}
	
	
	/**
	 * 检查SD卡是否可用
	 * @param ctx
	 * @param msg
	 * @return
	 */
	public static boolean isSdcardAvailable(){
		return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
	}
	
	
	/**
	 * 将字符串转换成整数
	 * @param str
	 * @return
	 */
	public static int parseInt(String str){
		int i;
		try {
			i = Integer.valueOf(str);
		} catch (NumberFormatException e) {
			i = 0;
		}
		return i;
	}
	
	/**
	 * 分转换成元
	 * @param fen
	 * @return
	 */
	public static String fenToYuan(int fen)
	{
		return df2.format(fen/100.00);
	}
	
}
